<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/9
 * Time: 14:26
 */

namespace App\Model\Account;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class TypeDetail extends Model
{
    //分类状态的列表
    public function typeDetailList($pageLimit, $where = [])
    {
        $list = DB::table('type_detail as td')
            ->leftJoin('type as t', 'td.td_tid', '=', 't.t_id')
            ->where($where)
            ->select('td_id', 't_name', 'td_name', DB::raw('from_unixtime(td_create_time) AS td_create_time'), DB::raw('from_unixtime(td_update_time) AS td_update_time'))
            ->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }
    //添加分类下面的种类
    public function typeDetailAdd($params)
    {
        $id =  DB::table('type_detail')->insertGetId([
            'td_tid'           => $params['td_tid'],
            'td_name'          => $params['td_name'],
            'td_create_time'   => time(),
            'td_update_time'   => time()
        ]);
        if($id){
            Redis::set('fenlei'.$params['td_tid'],null);
            Redis::set('zhonglei'.$id, $params['td_name']);
            return $id;
        }
        return false;
    }

    //修改分类状态
    public function typeDetailEdit($params)
    {
        $res = DB::table('type_detail')->where('td_id', $params['td_id'])->update([
            'td_tid'            =>   $params['td_tid'],
            'td_name'           =>   $params['td_name'],
            'td_update_time'    =>   time()
        ]);
        if(!$res){
            return false;
        }
        Redis::set('fenlei'.$params['td_tid'],null);
        Redis::set('zhonglei'.$params['td_id'], $params['td_name']);
        return $res;

    }

    //删除分类状态
    public function typeDetailDel($params)
    {
        $res =  DB::table('type_detail')->where('td_id', $params['td_id'])->delete();
        if(!$res){
            return false;
        }
        Redis::set('fenlei'.$params['td_tid'],null);
        Redis::set('zhonglei'.$params['td_id'], null);
        return $res;
    }

    //获取所有默认的分类状态
    public function getDetailAll()
    {
        return DB::table('type_detail')->where('td_branch_id', 0)->select('td_tid', 'td_name')->get();
    }











}